#include "StdAfx.h"
#include "ClientSocket.h"
#define  WM_SOCKET  WM_USER+1

ClientSocket::ClientSocket(void)
{
    InitSocket();
}


ClientSocket::~ClientSocket(void)
{
}

BOOL ClientSocket::InitSocket()
{
    WORD wVersionRequested;
    WSAData wsaData;
    int err;
    wVersionRequested=MAKEWORD(2,2);
    err=WSAStartup(wVersionRequested,&wsaData);
    if (0!=err)
    {
#ifdef _DEBUG
        ::OutputDebugString(_T("WSAStartup failed"));
#endif // _DEBUG
        return FALSE;
    }
    if (LOBYTE(wsaData.wVersion)!=2 || HIBYTE(wsaData.wVersion)!=2)
    {
#ifdef _DEBUG
        ::OutputDebugString(_T("WSAStartup version dismatch"));
#endif // _DEBUG
        WSACleanup();
        return FALSE;
    }

    return TRUE;
}


LRESULT CALLBACK WindowProc(
    __in  HWND hwnd,
    __in  UINT uMsg,
    __in  WPARAM wParam,
    __in  LPARAM lParam
    )
{
    switch(uMsg)
    {
    case WM_SOCKET:
        {
            SOCKET socket=(SOCKET)wParam;
            if (WSAGETSELECTERROR(lParam))
            {
                ::closesocket(socket);
                return 0;
            }
            switch(WSAGETSELECTEVENT(lParam))
            {
            case FD_READ:
                break;
            case FD_WRITE:
                break;
            case FD_CLOSE:
                break;
            }
        }
        return 0;
    default:
        break;
    }
    return ::DefWindowProc(hwnd,uMsg,wParam,lParam);
}

HWND ClientSocket::RegisterWindow()
{
    WNDCLASSEX wndclassex;
    wndclassex.cbSize=sizeof(wndclassex);
    wndclassex.cbClsExtra=0;
    wndclassex.cbWndExtra=0;
    wndclassex.hbrBackground=NULL;
    wndclassex.hCursor=NULL;
    wndclassex.hIcon=NULL;
    wndclassex.hIconSm=NULL;
    wndclassex.hInstance=GetModuleHandle(NULL);
    wndclassex.lpszClassName=_T("Message Window");
    wndclassex.lpfnWndProc=WindowProc;

    if (!RegisterClassEx(&wndclassex))
    {
        return NULL;
    }

    HWND hwnd=CreateWindowEx(0,_T("Message Window"),_T("Message Window"),0,0,0,0,0,NULL,NULL,GetModuleHandle(NULL),0);

    if (NULL==hwnd)
    {
        UnregisterClass(_T("Message Window"),GetModuleHandle(NULL));
        return NULL;
    }

    return hwnd;

}

BOOL ClientSocket::Connected(LPCSTR lpszServer, UINT nPort)
{
    socket_=::socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    msg_hwnd_=RegisterWindow();

    ::WSAAsyncSelect(socket_,msg_hwnd_,WM_SOCKET,FD_READ|FD_WRITE|FD_CLOSE);

    sockaddr_in sin;
    memset(&sin,0,sizeof(sin));
    sin.sin_family=AF_INET;
    sin.sin_port=htons(nPort);
    sin.sin_addr.S_un.S_addr=inet_addr(lpszServer);
    if(!::connect(socket_,(sockaddr*)&sin,sizeof(sin)))
    {
        ::closesocket(socket_);
        socket_=NULL;
        return FALSE;
    }
    return TRUE;
}

BOOL ClientSocket::SendMsg(char* buf)
{
    int result=send(socket_,buf,strlen(buf)*sizeof(char),0);
    if (SOCKET_ERROR==result)
    {
        return FALSE;
    }
    else
        return TRUE;
}
